package cn.sun.plane.action;

import cn.sun.plane.entity.Csn;
import cn.sun.plane.entity.CsnEntity;
import cn.sun.plane.entity.PageBean;
import cn.sun.plane.service.CsnService;
import cn.sun.plane.util.ExcelUtil2;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import javafx.application.Application;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class CsnAction extends ActionSupport implements ModelDriven<Csn>,ServletRequestAware,ServletResponseAware {

    private HttpServletRequest request;
    private HttpServletResponse response;
    private InputStream fileStream;


    //模型驱动使用的对象
    private Csn csn = new Csn();

    //当前页数
    private Integer currPage = 1;

    //注入部门管理的Service
    private CsnService csnService;

    @Override
    public Csn getModel() {
        return csn;
    }

    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    //提供一个查询的方法
    public String findAll() {
        PageBean<CsnEntity> pageBean = csnService.findByPage(currPage);
        //将pageBean 存入到值栈中
        ActionContext.getContext().getValueStack().push(pageBean);
        return "findAll";
    }

//    多条件查询方法
    public String findByOther(){
        csn  = Judge(csn);
        PageBean<CsnEntity> pageBean = csnService.findByOther(currPage,csn);
        ActionContext.getContext().getValueStack().push(pageBean);
        return "findAll";
    }

//    下载表格
    public void download() throws Exception{
        PrintWriter writer = response.getWriter();
        csn = Judge(csn);
        String title  = "航班查询结果";
        ExcelUtil2 excel = csnService.download(title, csn);
        try {
            excel.export(response);
        } catch (Exception e) {
            String a = URLEncoder.encode("导出出错，请稍后重试！", "UTF-8");
            writer.print("<script>alert(decodeURIComponent('" + a
                    + "') );window.location.href='robustPercentOne.html'</script>");
            writer.close();
        }
    }

//    判断查询条件
    private Csn Judge(Csn csn){
        HttpSession session = request.getSession();
        try {
            if(csn.getFd23() == null){
                csn = (Csn) session.getAttribute("Other");
            }else {
                session.setAttribute("Other",csn);
            }
        }catch (Exception e){
            csn = (Csn) session.getAttribute("Other");
        }
        return csn;
    }

    //用于跳转
    public String saveUI() {
        return "saveUI";
    }


    public void setCsn(Csn csn){
        this.csn = csn;
    }

    public void setCsnService(CsnService csnService) {
        this.csnService = csnService;
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    public InputStream getFileStream() throws UnsupportedEncodingException { return fileStream; }

    public void setFileStream(InputStream fileStream) { this.fileStream = fileStream; }

}
